package com.yutel.hdfs;

import java.io.InputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

public class CompressTest {

	// 压缩文件
	public static void compress(String codeClassName) throws Exception {
		Class<?> codecClass = Class.forName(codeClassName);
		System.setProperty("HADOOP_USER_NAME", "hadoop");
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.243:9000");
		FileSystem fs = FileSystem.get(conf);
		CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
		// 指定压缩文件路径
		FSDataOutputStream outputStream = fs.create(new Path("/eclipse/first.gz"));
		// 指定要被压缩的文件路径
		FSDataInputStream in = fs.open(new Path("/eclipse/first.txt"));
		// 创建压缩输出流
		CompressionOutputStream out = codec.createOutputStream(outputStream);
		IOUtils.copyBytes(in, out, conf);
		IOUtils.closeStream(in);
		IOUtils.closeStream(out);
	}

	// 解压缩
	public static void uncompress(String fileName) throws Exception {
		Class<?> codecClass = Class.forName("org.apache.hadoop.io.compress.GzipCodec");
		System.setProperty("HADOOP_USER_NAME", "hadoop");
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.243:9000");
		FileSystem fs = FileSystem.get(conf);
		CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
		
		FSDataInputStream inputStream = fs.open(new Path(fileName));
		// 把text文件里的数据解压，然后输出到控制台
		InputStream in = codec.createInputStream(inputStream);
		IOUtils.copyBytes(in, System.out, conf);
		IOUtils.closeStream(in);
	}

	public static void main(String[] args) throws Exception {
//		compress("org.apache.hadoop.io.compress.GzipCodec");
		uncompress("/eclipse/first.gz");
	}
}
